<template>
  <div>
    <type-nav />
    <!-- 主要内容区域 -->
    <div class="con">
      <!-- 导航路径区域 -->
      <div class="conPoin">
        <div class="conPoin">
          <span v-show="categoryView.category1Name">{{
            categoryView.category1Name
          }}</span>
          <span v-show="categoryView.category2Name">{{
            categoryView.category2Name
          }}</span>
          <span v-show="categoryView.category3Name">{{
            categoryView.category3Name
          }}</span>
        </div>
      </div>
      <!-- 主要内容区域 -->
      <div class="mainCon">
        <!-- 左侧放大镜区域 -->
        <div class="previewWrap">
          <!--放大镜效果-->
          <Zoom :skuImageList="skuImageList" />
          <!--下方的缩略图-->
          <image-list :skuImageList="skuImageList" />
        </div>
        <!-- 右侧选择区域布局 -->
        <div class="InfoWrap">
          <div class="goodsDetail">
            <h3 class="InfoName">{{ skuInfo.skuName }}</h3>
            <p class="news">{{ skuInfo.skuDesc }}</p>
            <div class="priceArea">
              <div class="priceArea1">
                <div class="title">
                  价&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;格
                </div>
                <div class="price">
                  <i>¥</i>
                  <em>{{ skuInfo.price }}</em>
                </div>
              </div>
            </div>
          </div>

          <div class="choose">
            <div class="chooseArea">
              <div class="choosed"></div>
              <dl v-for="spuSaleAttr in spuSaleAttrList" :key="spuSaleAttr.id">
                <dt class="title">{{ spuSaleAttr.saleAttrName }}</dt>
                <dd
                  v-for="spuSaleAttrValue in spuSaleAttr.spuSaleAttrValueList"
                  :key="spuSaleAttrValue.id"
                  :class="{ active: spuSaleAttrValue.isChecked == 1 }"
                  @click="
                    changeActive(
                      spuSaleAttrValue,
                      spuSaleAttr.spuSaleAttrValueList
                    )
                  "
                >
                  {{ spuSaleAttrValue.saleAttrValueName }}
                </dd>
              </dl>
            </div>
            <div class="cartWrap">
              <div class="controls">
                <input
                  autocomplete="off"
                  class="itxt"
                  v-model="skuNum"
                  @change="changeSkuNum"
                />
                <span class="plus" @click="skuNum++">+</span>
                <span class="mins" @click="skuNum > 1 ? skuNum-- : (skuNum = 1)"
                  >-</span
                >
              </div>
              <div class="add">
                <span @click="addShopcar">加入购物车</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    
  </div>
</template>
<script>
import { reqGoodsInfo, reqAddToCart } from "@/api";
import TypeNav from "@/components/TypeNav";
import ImageList from "./ImageList";
import Zoom from "./Zoom";
export default {
  name: "goodDetail",
  components: {
    TypeNav,
    ImageList,
    Zoom,
  },
  data() {
    return {
      categoryView: {},
      skuInfo: {},
      skuImageList: [],
      spuSaleAttrList: [],
      //购买产品的个数
      skuNum: 1,
    };
  },
  activated() {
    this.getGoodsInfo();
  },
  methods: {
    getGoodsInfo() {
      reqGoodsInfo(this.$route.params.skuId)
        .then((res) => {
          this.categoryView = res.data.categoryView;
          this.skuInfo = res.data.skuInfo;
          this.skuImageList = res.data.skuInfo.skuImageList;
          this.spuSaleAttrList = res.data.spuSaleAttrList;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    changeActive(saleAttrValue, arr) {
      arr.forEach((item) => {
        item.isChecked = 0;
      });
      saleAttrValue.isChecked = 1;
      console.log(saleAttrValue)
    },
    changeSkuNum(event) {
      let value = event.target.value * 1;
      if (isNaN(value) || value < 1) {
        this.skuNum = 1;
      } else {
        this.skuNum = parseInt(value);
      }
    },
    addShopcar() {
      const userInfo = this.$store.state.user.userInfo
      console.log(userInfo)
      if(!userInfo.id){
         this.$router.push('/login')
      }else{
        reqAddToCart(this.$route.params.skuId, this.skuNum)
        .then((res) => {
          if (res.code == 200) {
            console.log('成功')
            sessionStorage.setItem("SKUINFO", JSON.stringify(this.skuInfo))
            this.$router.push({name: 'addcartsuccess',query: {skuNum: this.skuNum}})
          }

        })
        .catch((err) => {
          console.log(err);
        });
      }

    },
  },
};
</script>
<style lang="less" scoped>
.con {
  width: 1200px;
  margin: 15px auto 0;
  .conPoin {
    padding: 9px 15px 9px 0;
    & > a + a:before {
      content: "/\00a0";
      padding: 0 5px;
      color: #ccc;
    }
    span{
      margin: 0 5px;
    }
  }
  .mainCon {
    overflow: hidden;
    margin: 5px 0 15px;
    .previewWrap {
      float: left;
      width: 400px;
      position: relative;
      .preview {
        width: 400px;
        height: 400px;
        border: 1px solid #dfdfdf;
        .jqzoom {
          cursor: pointer;
          width: 400px;
          height: 400px;
          position: relative;
          img {
            width: 100%;
          }
          .mask {
            width: 200px;
            height: 200px;
            background: rgba(255, 255, 255, 0.5);
            position: absolute;
            left: 0;
            top: 0;
            border: 1px solid #ddd;
          }
        }
        .maxbox {
          width: 400px;
          height: 400px;
          position: absolute;
          left: 420px;
          top: 0px;
          overflow: hidden;
          z-index: 20;
          border: 1px solid #ddd;
          img {
            width: 800px;
            height: 800px;
            display: block;
          }
        }
      }
      .specScroll {
        margin-top: 5px;
        width: 400px;
        overflow: hidden;
        .prev,
        .next {
          text-align: center;
          width: 10px;
          height: 54px;
          line-height: 54px;
          border: 1px solid #ccc;
          background: #ebebeb;
          cursor: pointer;
        }
        .prev {
          float: left;
          margin-right: 4px;
        }
        .next {
          float: right;
        }
        .items {
          float: left;
          position: relative;
          width: 372px;
          height: 56px;
          overflow: hidden;
          .itemsCon {
            position: absolute;
            width: 9999px;
            height: 56px;
            left: 0;
            img {
              float: left;
              text-align: center;
              border: 1px solid #ccc;
              padding: 2px;
              width: 50px;
              height: 50px;
              margin-right: 20px;
            }
          }
        }
      }
    }
    .InfoWrap {
      width: 700px;
      float: right;
      .InfoName {
        font-size: 14px;
        line-height: 21px;
        margin-top: 15px;
      }
      .news {
        color: #e12228;
        margin-top: 15px;
      }
      .priceArea {
        background: #fee9eb;
        padding: 7px;
        margin: 13px 0;
        .priceArea1 {
          overflow: hidden;
          line-height: 28px;
          margin-top: 10px;
          .title {
            float: left;
            margin-right: 15px;
          }
          .price {
            float: left;
            color: #c81623;
            i {
              font-size: 16px;
            }
            em {
              font-size: 24px;
              font-weight: 700;
            }
            span {
              font-size: 12px;
            }
          }
          .remark {
            float: right;
          }
        }
        .priceArea2 {
          overflow: hidden;
          line-height: 28px;
          margin-top: 10px;
          .title {
            margin-right: 15px;
            float: left;
          }
          .fixWidth {
            width: 520px;
            float: left;
            .red-bg {
              background: #c81623;
              color: #fff;
              padding: 3px;
            }
            .t-gray {
              color: #999;
            }
          }
        }
      }
      .support {
        border-bottom: 1px solid #ededed;
        padding-bottom: 5px;
        .supportArea {
          overflow: hidden;
          line-height: 28px;
          margin-top: 10px;
          .title {
            margin-right: 15px;
            float: left;
          }
          .fixWidth {
            width: 520px;
            float: left;
            color: #999;
          }
        }
      }
      .choose {
        .chooseArea {
          overflow: hidden;
          line-height: 28px;
          margin-top: 10px;
          .choosed {
            mark {
              height: 30px;
              display: inline-block;
              line-height: 30px;
              background-color: snow;
              border: 1px solid #ddd;
              padding: 0 20px;
              margin-right: 20px;
              span {
                font-size: 12px;
                color: red;
                margin-left: 20px;
                cursor: pointer;
              }
            }
          }
          dl {
            overflow: hidden;
            margin: 13px 0;
            dt {
              margin-right: 15px;
              float: left;
            }
            dd {
              float: left;
              margin-right: 5px;
              color: #666;
              line-height: 24px;
              padding: 2px 14px;
              border-top: 1px solid #eee;
              border-right: 1px solid #bbb;
              border-bottom: 1px solid #bbb;
              border-left: 1px solid #eee;
              cursor: pointer;
              &.active {
                color: red;
              }
            }
          }
        }
        .cartWrap {
          .controls {
            width: 78px;
            position: relative;
            float: left;
            margin-right: 15px;
            .itxt {
              width: 68px;
              height: 35px;
              border: 1px solid #ddd;
              color: #555;
              float: left;
              border-right: 0;
              text-align: center;
            }
            .plus,
            .mins {
              width: 15px;
              text-align: center;
              height: 17px;
              line-height: 17px;
              background: #f1f1f1;
              color: #666;
              position: absolute;
              right: -8px;
              border: 1px solid #ccc;
              cursor: pointer;
            }
            .mins {
              right: -8px;
              top: 19px;
              border-top: 0;
            }
            .plus {
              right: -8px;
            }
          }
          .add {
            float: left;
            span {
              background-color: #e1251b;
              padding: 0 25px;
              font-size: 16px;
              color: #fff;
              height: 36px;
              line-height: 36px;
              display: block;
              cursor: pointer;
            }
          }
        }
      }
    }
  }
}
.productDetail {
  width: 1200px;
  margin: 30px auto 0;
  overflow: hidden;
  .aside {
    width: 210px;
    float: left;
    border: 1px solid #ccc;
    .tabWraped {
      height: 40px;
      h4 {
        border-top: 3px solid #fff;
        float: left;
        line-height: 37px;
        width: 105px;
        text-align: center;
        border-bottom: 1px solid #ccc;
        &.active {
          border-top: 3px solid #e1251b;
          border-bottom: 0;
          font-weight: normal;
        }
      }
    }
    .tabContent {
      padding: 10px;
      .tab-pane {
        display: none;
        &.active {
          display: block;
        }
        &:nth-child(1) {
          .partList {
            overflow: hidden;
            li {
              width: 50%;
              float: left;
              border-bottom: 1px dashed #ededed;
              line-height: 28px;
            }
          }
          .goodsList {
            & > li {
              margin: 5px 0 15px;
              border-bottom: 1px solid #ededed;
              padding-bottom: 5px;
              .list-wrap {
                .p-img {
                  text-align: center;
                  img {
                    width: 152px;
                  }
                }
                .price {
                  font-size: 16px;
                  color: #c81623;
                }
                .operate {
                  text-align: center;
                  margin: 5px 0;
                  a {
                    background-color: transparent;
                    border: 1px solid #8c8c8c;
                    color: #8c8c8c;
                    display: inline-block;
                    padding: 2px 14px;
                    line-height: 18px;
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  .detail {
    width: 980px;
    float: right;
    .fitting {
      border: 1px solid #ddd;
      margin-bottom: 15px;
      .kt {
        border-bottom: 1px solid #ddd;
        background: #f1f1f1;
        color: #333;
        padding: 5px 0 5px 15px;
      }
      .good-suits {
        height: 170px;
        padding-top: 10px;
        .master {
          width: 127px;
          height: 165px;
          text-align: center;
          position: relative;
          float: left;
          img {
            width: 87px;
          }
          p {
            color: #c81623;
            font-size: 16px;
            font-weight: 700;
          }
          i {
            position: absolute;
            top: 48px;
            right: -25px;
            font-size: 16px;
          }
        }
        .suits {
          width: 668px;
          height: 165px;
          float: left;
          .suitsItem {
            float: left;
            width: 127px;
            padding: 0 20px;
            text-align: center;
            img {
              width: 120px;
              height: 130px;
            }
            p {
              font-size: 12px;
            }
            label {
              display: block;
              position: relative;
              input {
                vertical-align: middle;
              }
              span {
                vertical-align: middle;
              }
            }
          }
        }
        .result {
          border-left: 1px solid #ddd;
          width: 153px;
          height: 165px;
          padding-left: 20px;
          float: left;
          .num {
            font-size: 14px;
            margin-bottom: 10px;
            margin-top: 10px;
          }
          .price-tit {
            font-weight: bold;
            margin-bottom: 10px;
          }
          .price {
            color: #b1191a;
            font-size: 16px;
            margin-bottom: 10px;
          }
          .addshopcar {
            background-color: #e1251b;
            border: 1px solid #e1251b;
            padding: 10px 25px;
            font-size: 16px;
            color: #fff;
            display: inline-block;
            box-sizing: border-box;
          }
        }
      }
    }
    .intro {
      .tab-wraped {
        background: #ededed;
        // border: 1px solid #ddd;
        overflow: hidden;
        li {
          float: left;
          & + li > a {
            border-left: 1px solid #ddd;
          }
          &.active {
            a {
              // border: 0;
              background: #e1251b;
              color: #fff;
            }
          }
          a {
            display: block;
            height: 40px;
            line-height: 40px;
            padding: 0 11px;
            text-align: center;
            color: #666;
            background: #fcfcfc;
            border-top: 1px solid #ddd;
            border-bottom: 1px solid #ddd;
          }
        }
      }
      .tab-content {
        .tab-pane {
          display: none;
          &.active {
            display: block;
          }
          &:nth-child(1) {
            .goods-intro {
              padding-left: 10px;
              li {
                margin: 10px 0;
              }
            }
            .intro-detail {
              img {
                width: 100%;
              }
            }
          }
        }
      }
    }
  }
}
</style>